package cl.mt.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.LinkedList;

import cl.mt.dto.DTOPlan;
import cl.mt.util.bd.DatosTabla;
import cl.mt.util.bd.Parametro;

public class DataPlan extends DatosTabla {
	private static final String sqlGetId = "SELECT MAX(ID)+1 AS IDX FROM MASTRANSPORTE.MST_PLANES";
	private static final String sqlFindByPK = "SELECT ID, PLAT_ID, DESCRIPCION, FECHA_VIGENCIA FROM MASTRANSPORTE.MST_PLANES where ID=?";
	private static final String sqlFindAll = "SELECT ID, PLAT_ID, DESCRIPCION, FECHA_VIGENCIA FROM MASTRANSPORTE.MST_PLANES";
	private static final String sqlCreate = "INSERT INTO MASTRANSPORTE.MST_PLANES (ID, PLAT_ID, DESCRIPCION, FECHA_VIGENCIA) VALUES (?, ?, ?, ?)";
	private static final String sqlDeleteByPK = "DELETE FROM MASTRANSPORTE.MST_PLANES where ID=?";
	private static final String sqlUpdate = "UPDATE MASTRANSPORTE.MST_PLANES SET PLAT_ID=?, DESCRIPCION=?, FECHA_VIGENCIA=? where ID=?";


	private DTOPlan setDatosRegistro(ResultSet rs) throws SQLException {
		DTOPlan dto = new DTOPlan();
		dto.setId(formatInteger(rs.getObject("ID")));
		dto.setIdTipoPlan(formatInteger(rs.getObject("PLAT_ID")));
		dto.setDescripcion(formatString(rs.getObject("DESCRIPCION")));
		dto.setFechaVigencia(formatDate(rs.getDate("FECHA_VIGENCIA")));
		return dto;
	}

	public Integer generateId() {
		Parametro[] params = null;
		Integer id = null;
		try {
			getConnection();
			ResultSet rs = executeQuery(sqlGetId, params);
			while (rs.next()) {
				id = formatInteger(rs.getObject("IDX"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}
		return id;
	}
	public DTOPlan findByPrimaryKey(Long id) {

		DTOPlan dto = new DTOPlan();
		Parametro[] params = new Parametro[] { getParametro(id, Types.INTEGER)};
		try {
			getConnection();

			ResultSet rs = executeQuery(sqlFindByPK, params);
			while (rs.next()) {
				dto = setDatosRegistro( rs );
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return dto;
	}

	public Collection<DTOPlan> findAll() {

		Parametro[] params = null;
		Collection<DTOPlan> col = new LinkedList<DTOPlan>();
		try {
			getConnection();

			ResultSet rs = executeQuery(sqlFindAll, params);
			while (rs.next()) {
				DTOPlan dto = setDatosRegistro( rs );
				col.add( dto );
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return col;
	}

	public int deleteByPrimaryKey(Integer id) {

		Parametro[] params = new Parametro[] { getParametro(id, Types.INTEGER)};
		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlDeleteByPK, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return res;
	}

	public int create(DTOPlan dto) {

		Parametro[] params = new Parametro[] {
			getParametro(dto.getId(), Types.INTEGER),
			getParametro(dto.getIdTipoPlan(), Types.INTEGER),
			getParametro(dto.getDescripcion(), Types.VARCHAR),
			getParametro(formatTimestamp(dto.getFechaVigencia()), Types.TIMESTAMP)
		};

		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlCreate, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return res;
	}

	public int update(DTOPlan dto) {
		Parametro[] params = new Parametro[] {
			getParametro(dto.getIdTipoPlan(), Types.INTEGER),
			getParametro(dto.getDescripcion(), Types.VARCHAR),
			getParametro(formatTimestamp(dto.getFechaVigencia()), Types.TIMESTAMP),
			getParametro(dto.getId(), Types.INTEGER)
		};
		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlUpdate, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}
		return res;
}
}
